Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


However, you don’t have to wait for this updated software to be released. You can grab the JDBC API classes from the accompanying CD-ROM or from the JavaSoft Web site at http://splash.java.com/jdbc. As I was writing this chapter, the classes were stored in a file named “jdbc.100.tar.Z.” By the time you read this chapter, however, the file name may be slightly different. Once you have your software, simply follow these easy instructions to install the API classes in the proper place on your computer’s hard disk. The method shown here allows you to compile and run Java applications and applets (using the Appletviewer) that use the JDBC:

1.  Download the JDBC API package from the JavaSoft Web site or make a copy of the file from the CD-ROM.
2.  On your hard drive, locate the directory that stores the Java API packages. (On my PC, the directory is C:\JAVA\SRC, and on my Sun box, the directory is \usr\local\java\src.) You do not need to install the JDBC API package in the same directory as the rest of the Java API, but I strongly recommend that you do because, as I mentioned earlier, the JDBC API will soon be a standard part of the Java API distribution and will be packaged in the Java API hierarchy.
3.  Unpack the JDBC API classes using one of the following methods (for Unix-based machines or PCs), substituting the location where you downloaded the JDBC class file and the location where you want to install the JDBC classes.
Unix Procedure:
  To upack the file, enter prompt> uncompress \home\prpatel\jdbc.100.tar.Z.
  To create a jdbc directory with the classes and their source in separate directories, enter prompt> tar xvf \home\prpatel\jdbc.100.tar.Z.
  To install the JDBC classes, enter prompt> cd \usr\local\java\src, then enter prompt> mv \home\prpatel\jdbc\classes\java, and finally enter prompt> mv \home\prpatel\jdbc\src\java.

Windows 95 Procedure:
  Using a Windows 95 ZIP utility such as WinZip, uncompress and untar the file. Be sure the file name ends with .tar when you uncompress the file so that utilities will recognize the file. Untar the file to a tempory folder. Then do the following:
  Copy the java folder from the JDBC\CLASSES directory (from the temp directory where you untarred the downloaded file) to the C:\JAVA\SRC directory.
  Copy the java folder from the JDBC\SRC directory to C:\JAVA\SRC.
4.  Set the CLASSPATH to point to c:/usr/local/java/src (for Unix-based machines) or C:\JAVA\SRC (for PCs). Again, remember to substitute your location if this is not where you installed the downloaded file.


Tip:  Save the API documentation.
The only item left from the JDBC package you downloaded is the API documentation, which is in the jdbc\html directory that was created when you untarred the downloaded file. You may want to save that somewhere for reference. You can view the file using a Web browser.

I must stress that you should make sure that you have the CLASSPATH set properly. The package will be called in the following way in your Java program:

import  java.sql.*

You need to point the CLASSPATH at the parent of the java directory you copied in Step 2, which is why we set the CLASSPATH in Step 3. The package is contained in the java/sql/ folder, which is exactly as it should be according to the calling code snippet above.

Registering And Calling JDBC Drivers

Now that we’ve installed the JDBC classes, let’s cover how you load a JDBC driver. Note that the java.sql.* must be imported into your Java program if you want to use a JDBC driver. These JDBC base classes contain the necessary elements for properly instantiating JDBC drivers, and they serve as the “middleman” between you and the low-level code in the JDBC driver. The JDBC API provides you with an easy-to-use interface for interacting with data sources, independent of the driver you are using. The following sections cover three different ways to tell the JDBC’s DriverManager to load a JDBC driver.

The sql.drivers Property

When you want to identify a list of drivers that can be loaded with the DriverManager, you can set the sql.drivers system property. Because this is a system property, it can be set at the command line using the -D option:

java  -Dsql.drivers=imaginary.sql.iMsqlDriver classname

If there is more than one driver to include, just separate them using colons. If you do include more than one driver in this list, the DriverManager will look at each driver once the connection is created and decide which one matches the JDBC URL supplied in the Connection class’ instantiation. (I’ll provide more detail on the JDBC URL and the Connection class later on.) The first driver specified in the URL that is a successful candidate for establishing the connection will be used.

There’s Always A Class For A Name

You can explicitly load a driver using the standard Class.forName method. This technique is a more direct way of instantiating the driver class that you want to use in the Java program. To load the mSQL JDBC driver, insert this line into your code:

Class.forName("imaginary.sql.iMsqlDriver");

This method first tries to load the imaginary/sql/iMsqlDriver from the local CLASSPATH. It then tries to load the driver using the same class loader as the Java program—the applet class loader, which is stored on the network.

Just Do It

Another approach is what I call the “quick and dirty” way of loading a JDBC driver. In this case, you simply instantiate the driver’s class. Of course, I don’t advise you to take this route because the driver may not properly register with the JDBC DriverManager. The code for this technique, however, is quite simple and worth mentioning:

new  imaginary.sql.iMsqlDriver;

Again, if this is in the applet context, this code will first try to find this driver in the local CLASSPATH, then it will try to load it from the network.


Previous Table of Contents Next